#!/sbin/openrc-run

# init.d file for zoneminder
# Copyright (c) 2013-2022 Kaarle Ritvanen

name=zoneminder
command=/usr/bin/zmpkg.pl
extra_commands="setup"
CONFIG_FILE=/etc/zm/zm.conf

depend() {
	after mariadb
}

start() {
	ebegin "Starting $name"
	if status > /dev/null; then
		ewarn "$name already started"
	else
		zmlinkcontent.sh -z $CONFIG_FILE > /dev/null && $command start
	fi
	eend $?
}

stop() {
	ebegin "Stopping $name"
	if status > /dev/null; then
		$command stop
	else
		ewarn "$name already stopped"
	fi
	eend $?
}

status() {
	(
		. $CONFIG_FILE || exit
		chgrp $ZM_WEB_GROUP $CONFIG_FILE
		checkpath -d -m 755 -o $ZM_WEB_USER:$ZM_WEB_GROUP /var/cache/zoneminder
	)
	status=$($command status)
	echo $status
	[ "$status" = running ]
}

setup() {
	. $CONFIG_FILE
	ebegin "Initializing $name database"
	mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS \
		< $ZM_PATH_DATA/db/zm_create.sql
	eend $?
}
